package net.wanho.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.wanho.hospital.exception.AccountNotExistsException;
import net.wanho.hospital.exception.PasswordErrorException;
import net.wanho.hospital.po.Admin;
import net.wanho.hospital.mapper.AdminUserMapper;
import net.wanho.hospital.po.Patient;
import net.wanho.hospital.service.AdminUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.wanho.hospital.util.JwtUtil;
import net.wanho.hospital.vo.AdminVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 汤小洋
 * @since 2023-12-27
 */
@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, Admin> implements AdminUserService {

   /**
    *   按查询分页查询
    */
    @Override
    public IPage<Admin> findPage(int pageNum, int pageSize, Admin admin) {
        QueryWrapper<Admin> wrapper = null;
        IPage<Admin> page = new Page<>(pageNum, pageSize);
        page = this.page(page, wrapper);
        return page;
    }

    @Override
    public AdminVo login(int aId, String aPassword) {
        // 判断账户是否存在
        Admin admin = this.getById(aId);
        if (admin == null){
            throw new AccountNotExistsException("账户不存在！");
        }
        // 判断密码是否正确
        if (! admin.getAPassword().equals(aPassword)){
            throw new PasswordErrorException("密码错误！");
        }

        // 封装结果
        AdminVo adminVo = new AdminVo();
        adminVo.setAId(admin.getAId());
        adminVo.setAName(admin.getAName());

        Map<String,Object> payload = new HashMap<>();
        payload.put("aId", admin.getAId());
        payload.put("aName", admin.getAName());
        String token = JwtUtil.generate(payload);
        adminVo.setToken(token);

        return adminVo;
    }



}
